package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingBatchListBean;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class PickingOutStorageAddBatchAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStoragePickingBatchListBean.DataBean.BatchListBean> mBatchList;
    private LayoutInflater inflater;
    private PickingOutStorageAddBatchAdapterListener mPickingOutStorageAddBatchAdapterListener;

    public PickingOutStorageAddBatchAdapter(Context context, List<OutStoragePickingBatchListBean.DataBean.BatchListBean> batchList, PickingOutStorageAddBatchAdapterListener pickingOutStorageAddBatchAdapterListener) {
        mContext = context;
        mBatchList = batchList;
        mPickingOutStorageAddBatchAdapterListener = pickingOutStorageAddBatchAdapterListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mBatchList.size();
    }

    @Override
    public Object getItem(int position) {
        return mBatchList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OutStoragePickingBatchListBean.DataBean.BatchListBean batchItem = mBatchList.get(position);
        ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_out_storage_add_batch_select, null);
            viewHolder = new ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, batchItem);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.position = (TextView) view.findViewById(R.id.position);
        viewHolder.inventoryNumber = (TextView) view.findViewById(R.id.inventoryNumber);
        viewHolder.batch = (TextView) view.findViewById(R.id.batch);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, OutStoragePickingBatchListBean.DataBean.BatchListBean batchItem) {
        viewHolder.position.setText(batchItem.getPosition());
        viewHolder.inventoryNumber.setText(batchItem.getInventoryNumber() + batchItem.getUnit());
        viewHolder.batch.setText(batchItem.getBatch());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mPickingOutStorageAddBatchAdapterListener.onSelect(batchItem);
            }
        });
    }

    private class ViewHolder {
        private TextView position, inventoryNumber, batch;
    }

    public static abstract class PickingOutStorageAddBatchAdapterListener {
        public abstract void onSelect(OutStoragePickingBatchListBean.DataBean.BatchListBean item);
    }
}
